//
//  PhotographerViewController.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/14.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "PhotographerViewController.h"
#import "LuBDefine.h"
#import "PhotographerTableViewCell.h"
#import "LBAFNetworking.h"
#import "Photograph.h"
#import "DetailOfPhotographViewController.h"
#import <MJRefresh.h>

@interface PhotographerViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)NSMutableArray *photographArray;

@property (nonatomic, assign)NSInteger size;

@property (nonatomic, assign)BOOL isUpLoading;

@property (nonatomic, retain)NSArray *tempArray;

@end

@implementation PhotographerViewController

- (void)dealloc{
    [_tableView release];
    [_photographArray release];
    [_tempArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsCompact];
    [self createPhotographerViewController];
    // Do any additional setup after loading the view.
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.photographArray = [NSMutableArray array];
        self.tempArray = [NSArray array];
    }
    return self;
}

- (void)downloadData:(NSInteger)size{
    NSString *url = [NSString stringWithFormat:@"http://api.epetbar.com/gutouv2/star.html?limit=5&do=camera&city_id=0&page=%ld&develop=0",self.size];
    [LBNetWork handleGETWithUrlString:url parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        if (!self.isUpLoading) {
            [self.photographArray removeAllObjects];
        }
        self.tempArray = [NSArray arrayWithArray:[[responseObject objectForKey:@"data"] objectForKey:@"list"]];
        for (NSDictionary *dic in self.tempArray) {
            Photograph *photograph = [Photograph photographWithDictionary:dic];
            [self.photographArray addObject:photograph];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        // 没有更多数据
        if (self.tempArray.count == 0) {
            [self.tableView.footer endRefreshingWithNoMoreData];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

- (void)createPhotographerViewController{
    self.navigationItem.title = @"摄影师";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    UIImageView *bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
    [self.view addSubview:bgImageView];
    [bgImageView release];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 64, CWIDTH - 20, CHEIGHT - 64) style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    
    [self addHeaderRefresh];
    [self addFooterUploading];
}

- (void) addHeaderRefresh{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.size = 1;
        self.isUpLoading = NO;
        [self downloadData:self.size];
    }];
    [self.tableView.header beginRefreshing];
}

- (void) addFooterUploading{
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        self.size += 1 ;
        self.isUpLoading = YES;
        [self downloadData:self.size];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.photographArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PhotographerTableViewCell *cell = [PhotographerTableViewCell photographerTableViewCellWithTableView:tableView];

    cell.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
    if (self.photographArray.count) {
        cell.photograph = self.photographArray[indexPath.section];
    }else{
        
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (CHEIGHT == 480) {
        return CHEIGHT / 3.05;
    }if (CHEIGHT == 568) {
        return CHEIGHT /  3.6;
    }if (CHEIGHT == 667){
        return CHEIGHT / 4.1;
    }else{
        return CHEIGHT / 4.5;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, -10, CWIDTH, 5)];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
    [view release];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.tempArray.count == 0) {
    }else{
        DetailOfPhotographViewController *detailOfPhotographViewController = [[DetailOfPhotographViewController alloc]init];
        NSString *uid = [self.photographArray[indexPath.section] uid];
        NSString *petname = [self.photographArray[indexPath.section] petname];
        detailOfPhotographViewController.uid = uid;
        detailOfPhotographViewController.petname = petname;
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailOfPhotographViewController animated:YES];
        [detailOfPhotographViewController release];
    }
}

//页面边缘手势导航栏变不透明,tableView布局发生变化
- (void)viewDidAppear:(BOOL)animated{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsCompact];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
